package com.jwell.panrank.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jwell.driverapp.ui.base.BaseActivity;
import com.jwell.panrank.R;
import com.jwell.panrank.ui.adapter.ViewpagerAdapter;
import com.jwell.panrank.ui.custom.IndexViewPager;
import com.jwell.panrank.ui.fragment.inquire.CarNumberFragment;
import com.jwell.panrank.ui.fragment.inquire.InquireFragment;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class CarInquireActivityy extends BaseActivity {
    private IndexViewPager mViewPager;
    private ViewpagerAdapter adapter;
    private List<Fragment> list;

    private RadioGroup toolbarRadGro;
    private RadioButton toolbarRadBtnL;
    private RadioButton toolbarRadBtnR;
    private Toolbar toolbar;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_inquire);
        initToolbar();
        initView();
        setListener();
    }

    public void initView() {
        mViewPager = findViewById(R.id.viewPager);
        list = new ArrayList<>();
        list.add(new InquireFragment());
        list.add(new CarNumberFragment());
        List titles=new ArrayList<String>();
        adapter = new ViewpagerAdapter(getSupportFragmentManager(), list,titles);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setScanScroll(true);
        mViewPager.setAdapter(adapter);
    }
    private void initToolbar() {

        toolbar = findViewById(R.id.toolbar);
        toolbarRadGro = toolbar.findViewById(R.id.toolbar_radio_group);
        toolbarRadBtnL = toolbar.findViewById(R.id.toolbar_radio_button_left);
        toolbarRadBtnL.setText("非马坎");
        toolbarRadBtnR = toolbar.findViewById(R.id.toolbar_radio_button_right);
        toolbarRadBtnR.setText("马坎");
        toolbarRadGro.setVisibility(View.VISIBLE);
        toolbar.setNavigationIcon(R.mipmap.icon_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
    public void setListener() {
        toolbarRadGro.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.toolbar_radio_button_left:
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.toolbar_radio_button_right:
                        mViewPager.setCurrentItem(1);
                        break;
                    default:
                        break;
                }

            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        toolbarRadGro.check(R.id.toolbar_radio_button_left);
                        break;
                    case 1:
                        toolbarRadGro.check(R.id.toolbar_radio_button_right);
                        break;
                    default:
                        break;

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
